其实就是在Spring Cache基础上,增加的Redis配置,以及需要新建redis的配置类,其他不变和spring cache一致,在业务逻辑层配置@Cache注解。此时,所作的查询操作就会放到redis缓存中,可以在redis上查看,keys *查看key,get获取数据。
1.application.yml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15spring:
application:
name: spring-cache-redis
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/person?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
redis:
port: 6379
host: localhost
mybatis:
configuration:
cache-enabled: true
2.RedisConfig1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73package com.zhaixin.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
public class RedisConfig {
// 自定义RedisTemplate
"all") (
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
// 为了方便开发,一般直接使用<String,Object>
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// Json序列化配置
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
//String序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//key采取String的方式序列化
template.setKeySerializer(stringRedisSerializer);
// hash的key采用String方式序列化
template.setHashKeySerializer(stringRedisSerializer);
// value的序列化方式选择jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
public RedisCacheManager cacheManager(RedisConnectionFactory factory, RedisTemplate redisTemplate) {
// 设置过期时间,是否可以缓存空值
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration
.defaultCacheConfig()
.entryTtl(Duration.ofHours(1)) // 设置过期时间
.disableCachingNullValues() // 不允许有空值
// 序列化key,自动转String
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getKeySerializer()))
// 序列化value,将value转为json
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()));
RedisCacheManager cacheManager = RedisCacheManager.builder(RedisCacheWriter.lockingRedisCacheWriter(factory))
// 配置缓存
.cacheDefaults(cacheConfiguration)
// 配置同步修改或删除
.transactionAware()
.build();
return cacheManager;
}
}
3.启动类1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package com.zhaixin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
public class SpringcacheRedisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcacheRedisApplication.class, args);
}
}
参考:
https://zhuanlan.zhihu.com/p/138295935
https://my.oschina.net/itsaysay/blog/3011838